﻿Imports System.Data

Partial Class Departamentos
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            CargarDataListJefe()
            CargarGrillaDepartamentos()
            btnAceptar.Enabled = False
        End If
    End Sub

    Sub CargarDataListJefe()
        Dim ds As New DataSet
        ds = EmpleadosDB.EmpleadosNoJefesDepartamento
        With ddlJefe
            .DataSource = ds.Tables("Empleados")
            .DataValueField = "id_empleado"
            .DataTextField = "apellido_nombre"
            .DataBind()
            .Items.Insert(0, New ListItem("Sin Jefe", "0"))
        End With

    End Sub

    Sub CargarDataListJefeTodos()
        Dim ds As New DataSet
        ds = EmpleadosDB.GetEmpleados
        With ddlJefe
            .DataSource = ds.Tables("Empleados")
            .DataValueField = "id_empleado"
            .DataTextField = "apellido_nombre"
            .DataBind()
            .Items.Insert(0, New ListItem("Sin Jefe", "0"))
        End With

    End Sub
    Sub CargarGrillaDepartamentos()
        gvDepartamentos.DataSource = DepartamentoDB.GetDepartamentos
        gvDepartamentos.DataBind()
    End Sub

    Protected Sub gvDepartamentos_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvDepartamentos.PageIndexChanging
        gvDepartamentos.PageIndex = e.NewPageIndex
        CargarGrillaDepartamentos()
    End Sub

    Protected Sub gvDepartamentos_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvDepartamentos.RowDeleting
        Try
            DepartamentoDB.EliminarDepartamento(Convert.ToInt32(gvDepartamentos.DataKeys(e.RowIndex).Value.ToString()))
        Catch ex As Exception
            Session("Error") = ex.Message.ToString
            Session("Pagina") = "Departamentos.aspx"
            Server.Transfer("Errores.aspx")
        End Try

        CargarGrillaDepartamentos()

    End Sub

    Protected Sub gvDepartamentos_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvDepartamentos.SelectedIndexChanged
        txtIdDepartamento.Text = gvDepartamentos.SelectedDataKey.Values(0).ToString()
        txtDepartamento.Text = gvDepartamentos.SelectedRow.Cells(1).Text.ToString()
        CargarDataListJefeTodos()
        ddlJefe.SelectedValue = gvDepartamentos.SelectedDataKey.Values(1).ToString
        btnAceptar.Enabled = True

    End Sub

    Protected Sub btnNuevo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        btnNuevo.Enabled = False
        btnAceptar.Enabled = True
        btnCancelar.Enabled = True

        limpiar()
    End Sub

    Sub limpiar()
        txtIdDepartamento.Text = ""
        txtDepartamento.Text = ""
        ddlJefe.SelectedValue = 0
    End Sub

    Protected Sub btnAceptar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        Try
            If btnNuevo.Enabled = False Then 'es un nuevo depto
                DepartamentoDB.InsertarDepartamento(txtDepartamento.Text, ddlJefe.SelectedValue)
            Else 'es un update
                DepartamentoDB.ActualizarDepartamento(txtIdDepartamento.Text, txtDepartamento.Text, ddlJefe.SelectedValue)
            End If
            CargarGrillaDepartamentos()
            CargarDataListJefe()
            limpiar()
            gvDepartamentos.SelectedIndex = -1

        Catch ex As Exception
            Session("Error") = ex.Message.ToString()
            Session("Pagina") = "Departamentos.aspx"
            Server.Transfer("Errores.aspx")
        End Try
    End Sub

    Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        btnAceptar.Enabled = False
        btnNuevo.Enabled = True
        txtDepartamento.Text = ""
        txtIdDepartamento.Text = ""
    End Sub
End Class
